<!DOCTYPE html>
<html>

<body>

    <h1>call和apply函数</h1>
    <script>
	   /*=========== call 函数 ===========*/	
       /*1.person的fullName方法，应用于person1*/
	   var person = {
			fullName: function() {
				return this.firstName + " " + this.lastName;
			},
			fullName2: function(city ,country) {
				return city + " " + country;
			}
		}
		var person1 = {
			firstName:"Bill",
			lastName: "Gates",
		}
		console.log(person.fullName.call(person1));  // 输出 "Bill Gates"

		/*2.参数传递*/
		console.log(person.fullName2.call(person1,'jiangsu','china'));  // 输出 "jiangsu china"

		/*=========== apply 函数 ===========*/	
		/*1.apply接收数组参数*/
		var person3 = {
			fullName: function(city, country) {
				return this.firstName + " " + this.lastName + "," + city + "," + country;
			}
		}
		var person4 = {
			firstName:"Bill",
			lastName: "Gates"
		}
		console.log(person3.fullName.apply(person4, ["Oslo", "Norway"])); //输出 Bill Gates,Oslo,Norway

		/*2.通过apply使用 Math.max()方法*/
		console.log(Math.max.apply(null, [1,2,3])); // 输出 3
		
    </script>
</body>

</html>